package abg.core.common.message;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisMsgKit {

    private static StringRedisTemplate stringRedisTemplate;

    /**
     * 发送订阅消息
     * <p>
     * 例：RedisMsgKit.sendMsg(RedisMsgType.MT_CLOSE, "{}");
     *
     * @param msgType     消息类型对象
     * @param dataJsonStr 消息体：JSON格式字符串举例 {}
     */
    public static void sendMsg(RedisMsgType msgType, String dataJsonStr) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("channel", msgType.channel());
        jsonObject.put("type", msgType.type());
        jsonObject.put("desc", msgType.desc());
        jsonObject.put("data", dataJsonStr);
        stringRedisTemplate.convertAndSend(msgType.channel(), jsonObject.toString());
    }

    @Autowired
    public RedisMsgKit(StringRedisTemplate stringRedisTemplate) {
        RedisMsgKit.stringRedisTemplate = stringRedisTemplate;
    }

}
